<?php
namespace app\api\controller;

use app\admin\model\ProjectBind;
use app\common\controller\Api;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class Owner extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 申请成为车主
     *
     * @return void
     */
    public function apply()
    {
        $uid = $this->auth->id;
        $username = $this->request->post('username');
        $id_card = $this->request->post('id_card');
        $contact_number = $this->request->post('contact_number');
        $standby_telephone = $this->request->post('standby_telephone');
        $front_id_card_img = $this->request->post('front_id_card_img');
        $backend_id_card_img = $this->request->post('backend_id_card_img');
        $license_img = $this->request->post('license_img');
        if (!$username || !$id_card || !$contact_number || !$standby_telephone || !$front_id_card_img || !$backend_id_card_img || !$license_img) {
            $this->error('参数错误');
        }
        $userInfo = $this->auth->getUserInfo();
        if ($userInfo['cate'] == 1) {
            $this->error('您已经是车主，不能重复申请');
        }
        if ($userInfo['cate'] == 2 || $userInfo['cate'] == 3 || $userInfo['cate'] == 4) {
            $this->error('您存在其他身份，无法申请');
        }
        $data = [
            'uid' => $uid,
            'username' => $username,
            'id_card' => $id_card,
            'contact_number' => $contact_number,
            'standby_telephone' => $standby_telephone,
            'front_id_card_img' => $front_id_card_img,
            'backend_id_card_img' => $backend_id_card_img,
            'license_img' => $license_img,
        ];
        $res = (new \app\admin\model\Owner())->save($data);
        if ($res) {
            $this->success('申请成功, 请耐心等待审核');
        } else {
            $this->error('申请失败');
        }
    }

    /**
     * 个人信息
     *
     * @return void
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function userInfo()
    {
        $uid = $this->auth->id;
        $owner = (new \app\admin\model\Owner())->where('uid', $uid)->find();
        if ($owner) {
            $this->success('获取成功', $owner);
        }
        $this->error('获取失败');
    }

    /**
     * 任务列表
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public function taskList()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $projectModel = new \app\admin\model\Project();
        $list = $projectModel->page($page, $limit)->select();
        foreach ($list as $key => $value) {
            $list[$key]['company_name'] = (new \app\admin\model\User())->where('id', $value['company_id'])->value('username');
        }
        $count = $projectModel->count();
        $data = [
            'list' => $list,
            'count' => $count
        ];
        $this->success('获取成功', $data);
    }

    /**
     * 绑定项目
     *
     * @return void
     */
    public function bindProject()
    {
        $project_id = $this->request->post('project_id');
        $uid = $this->auth->id;
        $projectBindModel = (new ProjectBind());
        $data = [
            'uid' => $uid,
            'project_id' => $project_id,
        ];
        if ($projectBindModel->where('uid', $uid)->where('project_id', $project_id)->find()) {
            $this->error('该项目已经绑定');
        }
        $ownerModel = (new \app\admin\model\Owner());
        if (!$ownerModel->where('uid', $uid)->find()) {
            $this->error('请先申请成为车主');
        }
        $owner = $ownerModel->where('uid', $uid)->find();
        $data['owner_id'] = $owner['id'];
        $res = $projectBindModel->save($data);
        if ($res) {
            $this->success('绑定成功');
        } else {
            $this->error('绑定失败');
        }
    }

    /**
     * 我的项目
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function myProject()
    {
        $uid = $this->auth->id;
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $projectBindModel = (new ProjectBind());
        $list = $projectBindModel->where('uid', $uid)->page($page, $limit)->select();
        foreach ($list as $key => $value) {
            $list[$key]['project'] = (new \app\admin\model\Project())->where('id', $value['project_id'])->find();
            $list[$key]['today_orders'] = 0;
            $list[$key]['today_cars'] = 0;
            $list[$key]['total_orders'] = (new \app\admin\model\DeliveryOrder())->where('project_id', $value['project_id'])->count();
            $list[$key]['total_cars'] = 0;
        }
        $count = $projectBindModel->where('uid', $uid)->count();
        $data = [
            'list' => $list,
            'count' => $count
        ];
        $this->success('获取成功', $data);
    }
}